<?php
namespace app\app\util;
use think\cache\driver\Redis;
use think\Request;

//用户领取的红包的id的集合
define('RED_PACKET_ID','hmp:red_packet:id=');
//红包金额字符串
define('RED_PACKET_ARR','hmp:red_packet_arr:id=');
// 红包过期时间
define('PACKET_OVERDATETIME', 24*60*60);

class RedPacketUtils{

	//用户领取红包
	public static function getUserRedPacketId($user_id,$packet_id='')
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($packet_id != ''){
			// 领取红包,存入红包ID
			return $redis->sadd(RED_PACKET_ID.$user_id,$packet_id);
		}else{
			//查询
			return $redis->smembers(RED_PACKET_ID.$user_id);
		}
	}

	//生成红包/取出红包
	public static function createRedPacket($packet_id,$fee='',$type=1)
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($type == 1){
			if($fee != ''){
				//存入 红包集合
				$redis->sadd(RED_PACKET_ARR.$packet_id,$fee);
				//加入过期时间
				$redis->expire(RED_PACKET_ARR.$packet_id,OVERDATETIME);
				return true;
			}else{
				//取出红包
				return $redis->smembers(RED_PACKET_ARR.$packet_id);
			}
		}else{
			//去除红包成员
			$redis->srem(RED_PACKET_ARR.$packet_id,$fee);
			return true;
		}
	}

}